# -*- codeing = utf-8 -*-
# @Time : 2021/2/25 14:27
# @Atuthor: 朱朱
# @File: 088装饰器的几种类型.py
# @Software: PyCharm

# 1无参数的函数
# def check_time(fn):
#     def inner():
#         print('这是装饰开始前')
#         fn()
#         print('我是装饰开始后......')
#     return inner
#
# @check_time
# def go_to_bed():
#      print('去睡觉')
#
# go_to_bed()
# --------------------------------------

# 2有参数的函数
# def check_time(fn):
#     def inner(a,b):
#         print('这是装饰开始前')
#         fn(a,b)
#         print('我是装饰开始后......')
#     return inner
#
# @check_time
# def go_to_bed(a,b):
#      print('{}去{}睡觉'.format(a,b))
#
# go_to_bed("zhangsan","床上")
# ----------------------------------------------

# 3有不定长参数的函数
# def test(fn):
#     def inner(*args,**kwargs):
#         print('go....')
#         fn(*args,**kwargs)
#         print('end......')
#     return inner
#
# @test
# def demo(*args):
#     sum = 0
#     for x in args:
#         sum +=x
#     print(sum)
#
# demo(1, 2, 3, 4)
# ----------------------------------------
# 4装饰器中的return
# def test(fn):
#     def inner(*args,**kwargs):
#         print('go.........')
#         return fn(*args,**kwargs)  # 需要再这里写return语句，表示调用函数，获取函数的返回值并返回
#         print('end----------') # 前边已经返回了,这里调用不到了
#     return inner
#
# @test
# def demo(a,b):
#     return a + b
#
# print(demo(1, 2))  #3
# ---------------------------------------------------------

# 5 装饰器带参数 (在装饰器外面再加一层函数,类似闭包)
def outer_check(time):
    def check_time(fn):
        def inner():
            if time < 22:
                return fn() #惟一的条件,走到这里才可以装饰函数
            else:
                return '太晚了,赶紧睡觉吧'
        return inner
    return check_time

@outer_check(21)
def play_game():
    return '可以玩一会游戏,马上到点了'

print(play_game())